package com.zzyl.common.ai;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AIModelInvoker {
    @Autowired
    private BaiduAIProperties baiduAIProperties;
    public String qianfanInvoker(String prompt){
        System.out.println(prompt);
        //初始化一个 Qianfan 对象，配置其使用 OAuth 认证方式,并根据AccessKey和SecretKey连接千帆
        Qianfan qianfan=new Qianfan(Auth.TYPE_OAUTH, baiduAIProperties.getAccessKey(), baiduAIProperties.getSecretKey());
       //首先创建聊天请求
        ChatResponse response=qianfan.chatCompletion()
                //设置模型名称
                .model(baiduAIProperties.getQianfanModel())
                //添加用户消息
                .addMessage("user",prompt)
                //设置随机性程度
                .temperature(0.7)
                //设置最大输出token数
                .maxOutputTokens(2000)
                //设置响应格式
                .responseFormat("json_object")
                //发送请求并获取结果
                .execute();
        String result = response.getResult();
        System.out.println(result);
        return result;
    }

}
